#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_irq.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/platform_device.h>
#include <linux/miscdevice.h>

#define     GPIOLED_NAME    "miscled"
#define     LEDOFF          0
#define     LEDON           1
#define     MISCLED_MINOR  144

struct miscled_dev
{
    int minor;               /* 次设备号 */
    struct device_node *nd;  /* 设备节点 */
    struct device *device;   /* 设备     */
    int led_gpio;            
};

struct miscled_dev miscled;

static ssize_t miscled_write(struct file *pfile, 
                            const char __user *buf, size_t cnt, loff_t *ppos){
    int ret = 0;
    unsigned char databuf[1];
    struct miscled_dev *dev = pfile->private_data;
    ret = copy_from_user(databuf, buf, cnt);
    if(ret < 0) {
        printk("copy_from_user failed!\r\n");
        return -EINVAL;
    }
    if (databuf[0] == LEDON){
        gpio_set_value(dev->led_gpio, 0);
    }else if(databuf[0] == LEDOFF)
    { 
        gpio_set_value(dev->led_gpio, 1);
    }else
    {
        printk("command err! please input 0 or 1!\r\n");
        return -EINVAL;
    }

    return ret;
}

static int miscled_open(struct inode *inode, struct file *pfile){
    pfile->private_data = &miscled;
    return 0;
}

int miscled_release(struct inode *pinode, struct file *pfile){
    return 0;
}

static const struct file_operations miscled_fops= {
    .owner = THIS_MODULE,
    .write = miscled_write,
    .open = miscled_open,
    .release = miscled_release,
};

/* MISC设备结构体 */
static struct miscdevice led_miscdev = {
	.minor = MISCLED_MINOR,
	.name = GPIOLED_NAME,
	.fops = &miscled_fops,
};

static int __init miscled_init(void){
    int ret = 0;
    /* 获取设备节点 */
    miscled.nd = of_find_node_by_path("/gpioled");
    if (miscled.nd == NULL) {    /* 失败 */
		ret = -EINVAL;
		goto fail_findnode;
	}
    printk("find dts node successful!\r\n");

    /* 2, 获取LED所对应的GPIO */
    miscled.led_gpio = of_get_named_gpio(miscled.nd, "led-gpio", 0);
    if(miscled.led_gpio < 0) {
        printk("can't find led gpio\r\n");
        ret = -EINVAL;
        goto fail_findnode;
    }
    printk("led gpio num = %d\r\n", miscled.led_gpio);
    
     /* 3,申请IO */
    ret = gpio_request(miscled.led_gpio, "led-gpio");
	if (ret) {
		printk("Failed to request the led gpio\r\n");
		ret = -EINVAL;
        goto fail_findnode;
	}

    /* 4,使用IO，设置为输出 */
    ret = gpio_direction_output(miscled.led_gpio, 1);
	if (ret) {
		goto fail_setoutput;
	}

    /* 5操作gpio*/
    gpio_set_value(miscled.led_gpio, 0);

    ret = misc_register(&led_miscdev);
	if(ret < 0){
		printk("misc device register failed!\r\n");
		return -EFAULT;
	}

    return 0;

fail_setoutput:
    gpio_free(miscled.led_gpio);
fail_findnode:
    return ret;
}

static void __exit miscled_exit(void){

    /* 关灯 */
    gpio_set_value(miscled.led_gpio, 1);
    /* 释放IO */
    gpio_free(miscled.led_gpio);
    misc_deregister(&led_miscdev);
}

module_init(miscled_init);
module_exit(miscled_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("zhangkewei");